﻿/*
this file contains methods which are often used. It is included on all pages
*/

var messageCookieName = "DoeLibsMsg";

//sets the HttpMessage in the cookie. So this message will be persist over a redirect
function setHttpMessage(msg, success) {
    var val = {};
    val["Success"] = success;
    val["Message"] = msg;
    console.log(val);
    
    var json = JSON.stringify(val);
    console.log(json);

    createCookie(messageCookieName, json, 1);
}

//adds the message direct into the messagediv
function setHttpMessageDynamic(msg, success) {
    if (success) {
        $('#successModalDynamic div .modal-messageBody').text(msg);
        console.log(msg);
        $('#successModalDynamic').modal('show');
        setTimeout(function () { $('#successModalDynamic').modal('hide'); }, 3500);
    } else {
        $('#errorModalDynamic div .modal-messageBody').text(msg);
        console.log(msg);
        $('#errorModalDynamic').modal('show');
        setTimeout(function () { $('#errorModalDynamic').modal('hide'); }, 3500);
    }
}

function getHttpMessage() {
    var msg = [];

    var json = readCookie(messageCookieName);
    console.log(json);

    if (json == null)
        return null;

    json = JSON.parse(json);
    console.log(json);

    msg.Success = json.Success;
    msg.Message = json.Message;
    console.log(msg);

    return msg;
}

function createCookie(name, value, days) {
    var expires;

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = escape(name) + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = escape(name) + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) === 0) return unescape(c.substring(nameEQ.length, c.length));
    }
    return null;
}

function getFormData($form) {
    var unindexed_array = $form.serializeArray();
    var indexed_array = {};

    $.map(unindexed_array, function (n, i) {
        indexed_array[n['name']] = n['value'];
    });

    return indexed_array;
}

function clearAllErrorFields(formId) {
    console.log("start clean");
    $('span[data-valmsg-for]').text("");
    console.log("end clean");
    //$('#'+formId).each(function () {
    //    var fieldname = $(this).filter(':input').attr('name');
    //    console.log("Fieldname: " + fieldname);
    //    $('span[data-valmsg-for="' + fieldname + '"]').text("");
    //});
}

function addErrorsToFields(prefix, jsonErrorList) {
    $.each(jsonErrorList, function (key, message) {

        var qualifier = key.replace(prefix, "");
        console.log(qualifier);

        $('span[data-valmsg-for="' + qualifier + '"]').removeClass('field-validation-valid').addClass('field-validation-error').text(message);
    });
}

function getAuthorizationHeader(username, password) {
    var authType;

    if (password == "") {
        authType = "Cookie " + $.base64.encode(username);
    }
    else {
        var up = $.base64.encode(username + ":" + password);
        authType = "Basic " + up;
    };
    return authType;
}

function getStatusAsString(statusId) {
    switch(statusId) {
        case 0: return "Available"; 
        case 1: return "Borrowed";
        case 2: return "Reserved";
        case 3: return "Unavailable";
        case 4: return "Recalled";
        case 5: return "Deleted";
        case -1: return "Unknown";
    }
}